package demo.hdz.testopengl;

import android.util.Log;

import java.io.File;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 *
 * @author hdz
 * @date 2018/07/26
 */
public class Logger {
    private static final String TAG        = "TestOpenGL";
    private static final String CLASS_NAME = "demo.hdz.testopengl.Logger";

    private static boolean bSaveLog = true;
    private static boolean bOutLog  = true;
    public static String LOG_FILE_PATH = "/sdcard/debug.log";

    private static void log(int level, String tag, String log) {
        String sLogs = "";
        try {
            StackTraceElement[] a = new Throwable().getStackTrace();
            StackTraceElement ste = a[0];
            for (StackTraceElement anA : a) {
                if (!CLASS_NAME.equals(anA.getClassName())) {
                    ste = anA;
                    break;
                }
            }
            String  sClassName = ste.getClassName();
            String sMethodName = ste.getMethodName();
            int          iLine = ste.getLineNumber();
            String   sFileName = ste.getFileName();

            String sTmpClassName = "";
            int iPos = sClassName.lastIndexOf(".");
            if(iPos <=0 ) {
                sTmpClassName = sClassName;
            } else {
                sTmpClassName = sClassName.substring(iPos+1);
            }
            sLogs = "[" + sFileName + ":" + iLine + " " + sTmpClassName + "::" + sMethodName + "] " + log;
        } catch (Exception ex) {
            ex.printStackTrace();
            sLogs = log;
        }

        if (bSaveLog) {
            SimpleDateFormat df = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss:SSS]", new Locale("zh", "CN"));
            String        sTime = df.format(new Date());
            String         sLog = sTime + " " + getLogLevel(level) + " " + sLogs + "\n";
            Logger.save2File(sLog.getBytes(), LOG_FILE_PATH);
        }
        if (!bOutLog) {
            return;
        }

        switch (level) {
            case Log.DEBUG:
                Log.d(tag, sLogs);
                break;
            case Log.ERROR:
                Log.e(tag, sLogs);
                break;
            case Log.INFO:
                Log.i(tag, sLogs);
                break;
            case Log.VERBOSE:
                Log.v(tag, sLogs);
                break;
            case Log.WARN:
                Log.w(tag, sLogs);
                break;
            default:
                break;
        }
    }

    private static String getLogLevel(int level) {
        switch (level) {
            case Log.DEBUG:
                return "DEBUG";
            case Log.ERROR:
                return "ERROR";
            case Log.INFO:
                return "INFO";
            case Log.VERBOSE:
                return "VERBOSE";
            case Log.WARN:
                return "WARN";
            default:
                return "DEBUG";
        }
    }


    public static void save2File(byte[] b, String sFilePath) {
        try {
            File file = new File(sFilePath);
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    return;
                }
            }
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            raf.seek(file.length());
            raf.write(b);
            raf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void d(String tag, String log) {
        Logger.log(Log.DEBUG, tag, log);
    }
    public static void d(String tag, int log) {
        d(tag, "" + log);
    }
    public static void d(String tag, String log, boolean out) {
        if (out) {
            d(tag, log);
        }
    }
    public static void d(String tag, int log, boolean out) {
        if (out) {
            d(tag, log);
        }
    }
    public static void d(String log, boolean out) {
        if (out) {
            d(log);
        }
    }
    public static void d(int log, boolean out) {
        if (out) {
            d(log);
        }
    }
    public static void d(String log) {
        d(TAG, log);
    }
    public static void d(int log) {
        d(TAG, ""+log);
    }
    public static void d() {
        d(TAG, "======");
    }


    public static void e(String tag, String log) {
        Logger.log(Log.ERROR, tag, log);
    }
    public static void e(String tag, int log) {
        e(tag, "" + log);
    }
    public static void e(String tag, String log, boolean out) {
        if (out) {
            e(tag, log);
        }
    }
    public static void e(String tag, int log, boolean out) {
        if (out) {
            e(tag, log);
        }
    }
    public static void e(String log, boolean out) {
        if (out) {
            e(log);
        }
    }
    public static void e(int log, boolean out) {
        if (out)  {
            e(log);
        }
    }
    public static void e(String log) {
        e(TAG, log);
    }
    public static void e(int log) {
        e(TAG, ""+log);
    }
    public static void e() {
        e(TAG, "======");
    }


    public static void i(String tag, String log) {
        Logger.log(Log.INFO, tag, log);
    }
    public static void i(String tag, int log) {
        i(tag, "" + log);
    }
    public static void i(String tag, String log, boolean out) {
        if (out) {
            i(tag, log);
        }
    }
    public static void i(String tag, int log, boolean out) {
        if (out) {
            i(tag, log);
        }
    }
    public static void i(String log, boolean out) {
        if (out) {
            i(log);
        }
    }
    public static void i(int log, boolean out) {
        if (out)  {
            i(log);
        }
    }
    public static void i(String log) {
        i(TAG, log);
    }
    public static void i(int log) {
        i(TAG, ""+log);
    }
    public static void i() {
        i(TAG, "======");
    }
}
